package com.lancoo.examview.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.appcompat.app.AlertDialog;

import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.examview.R;
import com.lancoo.examview.widget.FontSizeView;

/**
 * Created by pengmi on 2019/11/5 19:15
 * Description:
 */
public class FontSizeChangeUtils {

    private static int size;
    private static long time;


    public static void showFontSizeChangeAlertDialog(final Context mContext,final FontSizeChangeCallBack fontSizeChangeCallBack) {
        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(AnswerConstant.SP_ANSWER_NAME, Context.MODE_PRIVATE);
        int defaultPosition = sharedPreferences.getInt(AnswerConstant.SP_FONT_SIZE, 1);
        size = defaultPosition;
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.lg_dialog_font_size_change, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.ev_dialogStyle);
        builder.setView(inflate);
        final AlertDialog alertDialog = builder.create();
        FontSizeView fontSizeView = inflate.findViewById(R.id.fsv_font_size);
        inflate.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();

            }
        });
        alertDialog.show();
        Window window = alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = DpUtils.getWidthInPx(mContext);
        window.setAttributes(attributes);
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.answerFill_DialogAnimation);
        fontSizeView.setDefaultPosition(defaultPosition);
        fontSizeView.setChangeCallbackListener(new FontSizeView.OnChangeCallbackListener() {
            @Override
            public void onChangeListener(int position) {
                size = position;
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putInt(AnswerConstant.SP_FONT_SIZE, position);
                edit.apply();
                if (fontSizeChangeCallBack != null) {
                    fontSizeChangeCallBack.upDateFontSize(position,getFontSizeForTextView(position, DpUtils.isPad(mContext)),getFontSizeForWebView(position,  DpUtils.isPad(mContext)));
                }

            }
        });
        alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (fontSizeChangeCallBack != null) {
                    fontSizeChangeCallBack.upDateAllFontSize(size,getFontSizeForTextView(size,  DpUtils.isPad(mContext)),getFontSizeForWebView(size,  DpUtils.isPad(mContext)));
                }
            }
        });

    }


    private static int getFontSizeForTextView(int fontIndex,boolean isPad){
        switch (fontIndex){
            case 0:
                return isPad?16:13;
            case 1:
                return isPad?18:15;
            case 2:
                return isPad?20:17;
            case 3:
                return isPad?22:19;
        }
        return 100;
    }

    public static int getFontSizeForWebView(int fontIndex,boolean isPad){
        switch (fontIndex){
            case 0:
                return isPad?100:80;
            case 1:
                return isPad?120:100;
            case 2:
                return isPad?140:120;
            case 3:
                return isPad?160:140;
        }
        return 100;
    }


    public interface FontSizeChangeCallBack{

        void upDateAllFontSize(int fontIndex,int textSize,int TextZoom);

        void upDateFontSize(int fontIndex,int textSize,int textZoom);
    }
}